package com.tobeing.voicechat.adapters;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tobeing.voicechat.R;
import com.tobeing.voicechat.customs.RecorderPlay;
import com.tobeing.voicechat.models.Recorder;

import java.util.List;

/**
 * Created by Administrator on 2017/10/30/030.
 */

public class RecorderAdapter extends BaseAdapter {
    private Context mContext;
    private List<Recorder> mRecorders;

    private int mMinLength;
    private int mMaxLength;

    public RecorderAdapter(Context context, List<Recorder> recorders){
        mContext = context;
        mRecorders = recorders;

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        mMinLength = (int) (metrics.widthPixels * 0.1f);
        mMaxLength = (int) (metrics.widthPixels * 0.8f);
    }

    @Override
    public int getCount() {
        return mRecorders == null ? 0 : mRecorders.size();
    }

    @Override
    public Recorder getItem(int position) {
        return mRecorders == null ? null : mRecorders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Recorder recorder = getItem(position);
        final ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.recorder_adapter, null);
            holder = new ViewHolder();
            holder.tvSecond = convertView.findViewById(R.id.tv_time);
            holder.flRecorder = convertView.findViewById(R.id.fl_recorder);
            holder.ivVoice = convertView.findViewById(R.id.iv_voice);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        int time = (int) getItem(position).getSecond();
        holder.tvSecond.setText(time + "\"");
        ViewGroup.LayoutParams lp = holder.flRecorder.getLayoutParams();
        if (time <= 60){
            lp.width = (int) (mMinLength + mMaxLength * time / 60f);
        }else {
            lp.width = mMaxLength;
        }

        holder.flRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.ivVoice.setBackgroundResource(R.drawable.voice_animation);
                AnimationDrawable ad = (AnimationDrawable) holder.ivVoice.getBackground();
                ad.start();
                //播放录制的音频
                RecorderPlay.startPaly(recorder.getPath(), new RecorderPlay.PlayCompletionListener() {
                    @Override
                    public void completed() {
                        //播放完成，停止动画效果
                        holder.ivVoice.setBackgroundResource(R.mipmap.adj);
                    }
                });
            }
        });

        return convertView;
    }

    class ViewHolder{
        public TextView tvSecond;
        public ImageView ivVoice;
        public FrameLayout flRecorder;
    }
}
